<?
include 'classes/GlobalConst.php';

function getPayingStatus($pStatus) {
	if ($pStatus == GlobalConst::$PAYING_STATUS) {
		return '<img src="images/m_pay.gif" border=0 alt="Paying" title="Paying" align=absmiddle>';
	}else if ($pStatus == GlobalConst::$WATTING_STATUS) {
		return '<img src="images/m_wait.gif" border=0 alt="Waiting" title="Waiting" align=absmiddle>';
	}else if ($pStatus == GlobalConst::$PROBLEM_STATUS) {
		return '<img src="images/m_prob.gif" border=0 alt="Problem" title="Problem" align=absmiddle>';
	}else if ($pStatus == GlobalConst::$NOT_PAYING_STATUS) {
		return '<img src="images/m_npay.gif" border=0 alt="Not Paying" title="Not Paying" align=absmiddle>';
	}
}
/*echo kieu withdrawl*/
function getWithDrawalType($withdrawal_type) {
	if ($withdrawal_type == GlobalConst::$MANUAL_WITHDRAWAL) {
		echo 'Manual';
	}else if ($withdrawal_type == GlobalConst::$INSTANCE_WITHDRAWAL) {
		echo 'Instant';
	}else if($withdrawal_type == GlobalConst::$AUTOMATIC_WITHDRAWAL){
		echo 'Automatic';
	}
}

$frm ['gid'] = intval ( $frm ['gid'] );
$q = 'select * from hl_groups ';
if (! ($sth = mysql_query ( $q ))) {
	exit ( mysql_error () );
}

$listings = array ();
$listing = '';
while ( $row = mysql_fetch_array ( $sth ) ) {
	if (($row ['id'] == 1 or $frm ['gid'] == $row ['id'])) {
		$listing = $row;
	}
	array_push ( $listings, $row );
}

$q = 'select count(*) as count 
				from
               hl_listings
        where
               group_id = ' . $listing ['id'] . ' order by id ';
if (! ($sth = mysql_query ( $q ))) {
	exit ( mysql_error () );
}

$row = mysql_fetch_array ( $sth );
$count_all = $row ['count'];
$onpage = 30;
$page = $frm ['p'];
$colpages = ceil ( $count_all / $onpage );
if ($page <= 1) {
	$page = 1;
}
if (($colpages < $page and 1 < $colpages)) {
	$page = $colpages;
}

$from = ($page - 1) * $onpage;
$q = 'select *, 
               date_format(date_added, \'%b %D, %Y\') as added,
               date_format(date_started, \'%b %D, %Y\') as started,
               date_format(date_closed, \'%b %D, %Y\') as closed,
               (date_added + interval ' . $settings ['new_for_days'] . ' day > current_date) as new,
               ((expiration != 0) && (date_added + interval expiration day < current_date)) as expired
        from
               hl_listings
        where
               group_id = ' . $listing ['id'] . ' and
               status != 2
        order by expired desc, id ';
if (! ($sth = mysql_query ( $q ))) {
	exit ( mysql_error () );
}

$listings = array ();
while ( $row = mysql_fetch_array ( $sth ) ) {
//	$q = 'select sum(amount * type) as payout, sum(amount * !type) as spend from hl_statistics where listing_id = ' . $row ['id'];
//	if (! ($ssth = mysql_query ( $q ))) {
//		exit ( mysql_error () );
//	}
//	
//	$srow = mysql_fetch_array ( $ssth );
//	$row ['ratio'] = ($srow ['spend'] != 0 ? sprintf ( '%.02f', $srow ['payout'] / $srow ['spend'] ) : '0.00');
//	$row ['spend'] = number_format ( $srow ['spend'], 2 );
	$votes_summary = array ('0' => 0, '1' => 0, '2' => 0, '3' => 0 );
	$votes_all = 0;
	$avg_vote = '0.0';
	$q = 'select count(*) as cvotes, vote from hl_votes where listing_id = ' . $row ['id'] . ' and confirm = \'0\' group by vote';
	if (! ($ssth = mysql_query ( $q ))) {
		exit ( mysql_error () );
	}
	
	while ( $srow = mysql_fetch_array ( $ssth ) ) {
		$votes_all += $srow ['cvotes'];
		$votes_summary [$srow ['vote']] = $srow ['cvotes'];
	}
	
	if (0 < $votes_all) {
		$avg_vote = sprintf ( '%.01f', ($votes_summary ['3'] * 10 + $votes_summary ['2'] * 5 - $votes_summary ['1'] * 0 - $votes_summary ['0'] * - 5) / $votes_all );
	}
	
	$row ['avg_vote'] = $avg_vote;
	$row ['cvotes'] = $votes_all;
	$q = 'select sum(`in`) as tin, sum(`out`) as tout from hl_traffic where listing_id = ' . $row ['id'] . ' and date + interval ' . $settings ['traffic_count_days'] . ' day > now()';
	if (! ($ssth = mysql_query ( $q ))) {
		exit ( mysql_error () );
	}
	
	$traffic = mysql_fetch_array ( $ssth );
	$row ['in'] = intval ( $traffic ['tin'] );
	$row ['out'] = intval ( $traffic ['tout'] );
	$row ['traffic_ratio'] = ($traffic ['tout'] == 0 ? '0.0' : sprintf ( '%.01f', $traffic ['tin'] / $traffic ['tout'] ));
	array_push ( $listings, $row );
}

$listings = array_splice ( $listings, $from, $onpage );
echo '<script language="javascript"><!--

function editVotes(id)
{
  w = 400; h = 600;
  t = (screen.height-h-30)/2;
  l = (screen.width-w-30)/2;
  window';
echo '.open(\'?a=edit_votes&lid=\' + id, \'edit_votes\' + id, "top="+t+",left="+l+",width="+w+",height="+h+",resizable=1,scrollbars=1");
}
--></script>';
?>
<form name="grps">
	<input type=hidden name=a value=listings>
	<table cellspacing=1 cellpadding=2 border=0 width=100%>
		<tr>
  		<td><b>Listings: <?php echo $listing ['name'];?> (<?php echo $listing ['type'];?> )</b></td>
 			<td width=1% nowrap>
				<select name="gid" class=inpts onchange="document.grps.submit()">
				<?php foreach ( $listings as $grp ) {?>
					<option value="<?php echo $grp ['id'];?>" <?php echo ($grp ['id'] == $listing ['id'] ? 'selected' : '');?> >
						<?php echo $grp ['name'];?>
						<?php echo ($grp ['status'] ? '' : ' (disabled)');?>
					</option>
				<?php }?>
				</select> 
				<input type="submit" value="GO" class=sbmt></td>
		</tr>
	</table>
</form>
<table cellspacing=1 cellpadding=2 border=0 width=100%>
<?php if (0 < sizeof ( $listings )) {
	foreach ( $listings as $list ) {
		$payments = preg_split ( '/,/', $list ['pay_systems'] );
?>		
	<tr>
 		<td>
  		<table cellspacing=0 cellpadding=1 border=0 width=100%>
  			<tr>
  				<td bgcolor=#808000>
   					<table cellspacing=0 cellpadding=2 border=0 bgcolor=#FFFFFF width=100%>
    					<tr>
     						<td valign=top width=33% >
       						<a href="<?php echo $list ['url'];?>" target=_blank><b><?php echo $list ['name'];?> </b></a>
									<?php echo ($list ['new'] ? '<b style="color: red"><i>new</i></b>' : '');?>
									<?php echo ($list ['expired'] ? '<b style="color: red">expired</b>' : '');?>
								<br>
       					Status: <b><?php echo ($list ['status'] ? 'On' : 'Off');?></b>
       					<br>
       					Paying Status:<?php echo getPayingStatus($list ['hyip_status']);?>
								<br/>
								<a href="mailto:<?php echo $list ['email'];?>">Contact</a><br>
       					<br>
     					</td>
     <td valign=top width=33% nowrap>
       <b><?php echo $list ['percents'];?> </b>
       <br> Minimal Spend: <b><?php echo $list ['min_spend'];?></b>
       <br> Maximal Spend: <b><?php echo ($list ['max_spend'] ? $list ['max_spend'] : 'No Limit');?></b>
       <br> Referral:  <b><?php echo ($list ['referral'] ? $list ['referral'] : 'No');?></b>
       <br> Withdrawal: <b><?php echo getWithDrawalType($list ['withdrawal_type']);?></b>
       <br>
       <br>
     </td>
     <td valign=top width=33% nowrap>
       Our Rating: 
    <?php    
		for($i = 1; $i <= 5; ++ $i) {
			if ($i <= intval ( $list ['rating'] )) {
				echo '<img src="images/full_star.gif" align=absmiddle>';
				continue;
			} else {
				echo '<img src="images/empty_star.gif" align=absmiddle>';
				continue;
			}
		}
		?>
		<br> Votes: <b><?php echo $list ['avg_vote'];?>-<?php echo $list ['cvotes'];?> votes</b>
		<br> 
		<a href="javascript:editVotes('<?php echo $list ['id'];?>')">Manage Votes</a><br>
		<?php if ((((($list ['support_email'] or $list ['support_form']) or $list ['support_forum']) or $list ['support_phone']) or $list ['support_aim'])) {?>
			Support:
			<?php if ($list ['support_email']) {?>
				<a href="mailto:<?php echo $list ['support_email'];?>"><img src="images/smail.gif" border=0 alt="Support E-Mail" title="Support E-Mail" align=absmiddle></a>
			<?php }?>
			<?php if ($list ['support_form']) {?>
				<a href="<?php echo $list ['support_form'];?>" target=_blank><img src="images/sform.gif" border=0 alt="Support Form" title="Support Form" align=absmiddle></a>
			<?php }?>
			<?php if ($list ['support_forum']) {?>
				<a href="<?php echo $list ['support_forum'];?>" target=_blank><img src="images/sforum.gif" border=0 alt="Support Forum" title="Support Forum" align=absmiddle></a>
			<?php }?>
			<?php if ($list ['support_phone']) {?>
				<img src="images/sphone.gif" border=0 alt="Phone:<?php echo $list ['support_phone'];?>" title="Phone:<?php echo $list ['support_phone'];?>" align=absmiddle>
			<?php }?>
			<?php if ($list ['support_aim']) {?>
				<img src="images/smsn.gif" border=0 alt="<?php echo $list ['support_aim'];?>" title="<?php echo $list ['support_aim'];?>" align=absmiddle>
			<?php }?>
			<br>
		<?php }?>
		
		Added: <b><?php echo $list ['added'];?></b>
		<br/>
		Started: <b><?php echo $list ['started'];?></b>
		<br>
    In: <b><?php echo $list ['in'];?></b> 
    Out: <b><?php echo $list ['out'];?></b> 
    Ratio: <b><?php echo $list ['traffic_ratio'];?></b>
     <br>
   </td>
     <td valign=top width=33%>
		<a href="?a=edit_listing&lid=<?php echo $list ['id'];?>&gid=<?php echo $frm ['gid'];?>&p=<?php echo $frm ['p'];?>">[edit]</a><br> 
		<a href="?a=delete_listing&lid=<?php echo $list ['id'];?>&gid=<?php echo $frm ['gid'];?>&p=<?php echo $frm ['p'];?>" onclick="return confirm('Do you really want to delete this listing?')">[delete]</a>
		<br>
    <br>
   </td>
    </tr>
    <tr>
      <td colspan=4 nowrap>
				<?php foreach ( $payments as $pay ) {?>
				<img src="images/<?php echo $pay;?>.gif" align=absmiddle alt="<?php echo $pay;?>" title="<?php echo $pay;?>">
			<?php }?>
			</td>
    </tr>
   </table>
  </td></tr></table>
 </td>
</tr>
<?php 
	}
} else {?>
	<tr>
    <td bgcolor=FFF9B3 colspan=4>No Listings found.</td>
</tr>
<?php }?>
</table>
<br>
<form method=post>
	<input type="hidden" name="a" value="add_listing">
	<input type="hidden" name="gid" value="<?php echo $frm ['gid'];?>">
	<input type="submit" value="Add Listing" class=sbmt>
</form>
<center>
<?php if (1 < $colpages) {
	if (1 < $page) {
		echo ' <a href="?a=listings&gid=';
		echo $frm ['gid'];
		echo '&p=';
		echo $page - 1;
		echo '">&lt;&lt;</a> ';
	}
	
	for($i = 1; $i <= $colpages; ++ $i) {
		if ($i == $page) {
			echo ' <b>';
			echo $i;
			echo '</b> ';
			continue;
		} else {
			echo ' <a href="?a=listings&gid=';
			echo $frm ['gid'];
			echo '&p=';
			echo $i;
			echo '">';
			echo $i;
			echo '</a> ';
			continue;
		}
	}
	
	if ($page < $colpages) {
		echo ' <a href="?a=listings&gid=';
		echo $frm ['gid'];
		echo '&p=';
		echo $page + 1;
		echo '">&gt;&gt;</a> ';
	}
}
?>
</center>
<br>
